﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of HourglassCtrl class
	/// The hourglass control, an animated panel showing FRL logo
	/// </summary>
	public partial class HourglassCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Attributes

		private Storyboard m_storyboardA = null;
		private Storyboard m_storyboardB = null;
		private Storyboard m_storyboardC = null;

		private int m_iShowCount = 0;

		private int m_iTicks;

		/////////////////////////////////////////////////////////////
		// Construction

		public HourglassCtrl()
		{

			// Load xaml resources
			InitializeComponent();

			// Get the storyboard
			m_storyboardA = Resources["animBlockA"] as Storyboard;
			m_storyboardB = Resources["animBlockB"] as Storyboard;
			m_storyboardC = Resources["animBlockC"] as Storyboard;

			// Set trigger events
			m_storyboardA.Completed += new EventHandler(StoryboardA_Completed);
			m_storyboardB.Completed += new EventHandler(StoryboardB_Completed);
			m_storyboardC.Completed += new EventHandler(StoryboardC_Completed);

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		void StoryboardA_Completed(object sender, EventArgs e)
		{

			// Shortcuts
			if (m_iShowCount <= 0)
				return;

			// Start animation B
			m_storyboardB.Begin();

		}

		void StoryboardB_Completed(object sender, EventArgs e)
		{

			// Shortcuts
			if (m_iShowCount <= 0)
				return;

			// Start animation B
			m_storyboardC.Begin();

		}

		void StoryboardC_Completed(object sender, EventArgs e)
		{

			// Shortcuts
			if (m_iShowCount <= 0)
				return;

			// Start animation B
			m_storyboardA.Begin();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public void IncShowCount(bool bInc)
		{

			// Increment and decrement a counter to allow for
			// nested calls

			// Update count
			if (bInc)
				m_iShowCount++;
			else
				m_iShowCount--;
#if DEBUG
			Debug.Assert(m_iShowCount >= 0);
#endif // #if DEBUG

			// If starting...
			if ((m_iShowCount == 1) && (bInc))
			{

				// Debug dump
#if DEBUG
				Debug.WriteLine("Showing hourglass");
#endif // #if DEBUG

				// Stop animations
				if (m_storyboardA.GetCurrentState() != ClockState.Stopped)
					m_storyboardA.Stop();
				if (m_storyboardB.GetCurrentState() != ClockState.Stopped)
					m_storyboardB.Stop();
				if (m_storyboardC.GetCurrentState() != ClockState.Stopped)
					m_storyboardC.Stop();

				// Reset positions
				rotA.Angle = 0;
				rotB.Angle = 0;
				rotC.Angle = 0;

				// Show the controls
				this.Visibility = System.Windows.Visibility.Visible;

				// Start storyboard A
				m_storyboardA.Begin();

				// Update tick when control is shown
				m_iTicks = Environment.TickCount;

			}
			else if ((m_iShowCount == 0) && (!bInc))
			{

				// Debug dump
#if DEBUG
				Debug.WriteLine("Hiding hourglass");
#endif // #if DEBUG

				// Hide the controls
				this.Visibility = System.Windows.Visibility.Collapsed;

				// Stop animations
				if (m_storyboardA.GetCurrentState() != ClockState.Stopped)
					m_storyboardA.Stop();
				if (m_storyboardB.GetCurrentState() != ClockState.Stopped)
					m_storyboardB.Stop();
				if (m_storyboardC.GetCurrentState() != ClockState.Stopped)
					m_storyboardC.Stop();

			}

		}

	}

}
